
import { getProductList } from '../../services/goods';
Page({
  data: {
    categoryId: 0,
    productname: '',
    sortWay: 0,
    sortType: 0,
    defaultTitle: "下拉刷新",
    pullRefresh: true,
    tabIndex: 0,
    totalpage: 1,
    show: false, // 是否显示加载动画
    page: 1, // 当前页数
    list: [], // 页面List数据
    showRight: false,
    BrandList: [{ 'name': '华为' }, { 'name': '华为' }, { 'name': '华为' }, { 'name': '华为' }],
    logList: [{ 'name': '包邮' }, { 'name': '到付' }, { 'name': '自提' }]
  },
  onLoad(event) {
    if (event.productname) {
      this.setData({ productname: event.productname })
    }
    if (event.categoryId) {
      this.setData({ categoryId: event.categoryId })
    }
    // this.mySchedulde();
    this.getProductList()
  },
  onShow() {
    my.setNavigationBar({
      title: '产品列表',
      backgroundColor: '#4BA3F9'
    })
  },
  searchgoods(e) {
    this.setData({ productname: e.detail.value })
    my.startPullDownRefresh()
  },
  tabClick(event) {
    this.setData({ tabIndex: event.target.dataset.num });
    this.setData({ sortType: event.target.dataset.num })
    my.startPullDownRefresh()
  },
  filtrate() {
    this.setData({ showRight: true });
  },
  onTopBtnTap() {
    this.setData({
      showRight: true,
    });
  },
  onPopupClose() {
    this.setData({
      showRight: false,
    });
  },
  onPullDownRefresh() {
    let that = this
    this.setData({
      list: [],
      page: 1,
      pages: 1
    });
    setTimeout(function () {
      that.getProductList()
    }, 1000)
  },
  stopPullDownRefresh() {
    my.stopPullDownRefresh({
      complete(res) {
        console.log(res, new Date())
      }
    })
  },
  async getProductList() {
    let param = {
      sortType: this.data.sortType,
      sortWay: this.data.sortWay,
      pageNum: this.data.page,
      pageSize: 20
    }
    if (this.data.categoryId > 0) {
      param.categoryId = this.data.categoryId;
    }
    if (this.data.productname.length > 0) {
      param.productName = this.data.productname;
    }
    let that = this
    getProductList(param).then((res) => {
      console.log(res)
      if (res.code == "000000") {
        this.setData({ totalpage: res.data.pages });
        this.setData({
          list: this.data.list.concat(res.data.list),
          show: false
        });

      }
      this.stopPullDownRefresh()
    })

  },
  /**
  * scroll-view滑到底部触发事件
  * @method scrollMytrip
  */
  async scrollMytrip() {
    try {
      // 判断是否还有数据需要加载
      if (this.data.page < this.data.pages) {
        this.setData({ show: true });
        this.setData({ page: this.data.page + 1 });
        this.getProductList();
      }
    } catch (e) {
      this.setData({ show: false });
      console.log('scrollMytrip执行异常:', e);
    }
  }
});
